package com.hangwei.paperless.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.DRSInfo;
import com.hangwei.paperless.widget.MyTextWatcher;
import com.twelve.all.utils.LogUtil;
import com.twelve.all.utils.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张龙臣 on 2020/10/23.
 */

public class DRSAdapter extends RecyclerView.Adapter<DRSAdapter.MyViewHolder> {
    private final boolean mIsCurrent;
    private List<DRSInfo.DrsBean> mData;
    private Context mContext;
    private boolean mCanNa; //检验员盖章 没盖章可以选择NA

    public DRSAdapter(boolean isCurrent) {
        mIsCurrent = isCurrent;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mContext = viewGroup.getContext();
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_drs, viewGroup, false);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int i) {
        holder.setIsRecyclable(false);
        holder.checkbox.setEnabled(mIsCurrent && mCanNa);
        holder.mLl.setEnabled(mIsCurrent && mCanNa);
        final DRSInfo.DrsBean bean = mData.get(i);
        holder.etContent.setEnabled(mIsCurrent && mCanNa && !bean.isNA);
        holder.tvTitle.setText(Tools.checkedString(bean.seq + " " + bean.title));
        holder.etContent.setText(Tools.checkedString(bean.actual));
        holder.tvPsi.setText(Tools.checkedString(bean.measureUnitName));
//        holder.etContent.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
        holder.checkbox.setChecked(bean.isNA);
        final MySelectAdapter adapter = new MySelectAdapter(bean.selectItem, bean.selectValue);
        if (bean.selectItem != null && bean.selectItem.size() > 0) {
            holder.mRecyclerView.setVisibility(View.VISIBLE);
            holder.mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
            holder.mRecyclerView.setAdapter(adapter);
        } else
            holder.mRecyclerView.setVisibility(View.GONE);
        if ("文本".equals(bean.valueTypeName)) {
            holder.tvLimit.setVisibility(View.GONE);
            holder.etContent.setVisibility(View.VISIBLE);
        } else if ("自定义选项".equals(bean.valueTypeName)) {
            holder.etContent.setVisibility(View.GONE);
            holder.tvLimit.setVisibility(View.GONE);
        } else {
            holder.tvLimit.setVisibility(View.VISIBLE);
            holder.etContent.setVisibility(View.VISIBLE);
            StringBuilder builder = new StringBuilder();
            boolean hasMin = !TextUtils.isEmpty(bean.min);
            boolean hasMax = !TextUtils.isEmpty(bean.max);
            builder.append("限定：");
            if (hasMin)
                builder.append("Min:").append(bean.min);
            if (hasMin && hasMax)
                builder.append("-");
            if (hasMax)
                builder.append("Max:").append(bean.max);
            holder.tvLimit.setText(builder.toString());
        }
        holder.etContent.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                bean.actual = s.toString().trim();
            }
        });
        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                bean.isNA = isChecked;
                holder.etContent.setText("");
                holder.etContent.setEnabled(!isChecked);
                adapter.setEnable(!isChecked);
            }
        });
        holder.mLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checked = holder.checkbox.isChecked();
                if (checked) {
                    holder.checkbox.setChecked(false);
                    holder.etContent.setEnabled(true);
                    adapter.setEnable(true);
                } else {
                    holder.checkbox.setChecked(true);
                    holder.etContent.setText("");
                    holder.etContent.setEnabled(false);
                    adapter.setEnable(false);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mData == null)
            return 0;
        return mData.size();
    }

    public void setData(List<DRSInfo.DrsBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void setCanNa(boolean canNa) {
        mCanNa = canNa;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        public CheckBox checkbox;
        public TextView tvTitle;
        public EditText etContent;
        public TextView tvPsi;
        public TextView tvLimit;
        public LinearLayout llLimit;
        private LinearLayout mLl;
        private RecyclerView mRecyclerView;

        MyViewHolder(@NonNull View itemView) {
            super(itemView);
            checkbox = itemView.findViewById(R.id.checkbox);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            etContent = itemView.findViewById(R.id.etContent);
            tvPsi = itemView.findViewById(R.id.tvPsi);
            llLimit = itemView.findViewById(R.id.llLimit);
            tvLimit = itemView.findViewById(R.id.tvLimit);
            mLl = itemView.findViewById(R.id.ll);
            mRecyclerView = itemView.findViewById(R.id.recyclerView);
        }
    }

    static class MySelectAdapter extends RecyclerView.Adapter {

        private final List<String> mSelectItem;
        private List<String> mSelectValue;
        private boolean mEnable = true;

        public MySelectAdapter(List<String> selectItem, List<String> selectValue) {
            mSelectItem = selectItem;
            if (selectValue == null)
                mSelectValue = new ArrayList<>();
            mSelectValue = selectValue;
            if (mSelectValue.size() == 0)
                mSelectValue.add(null);
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_item_drs, viewGroup, false);
            return new RecyclerView.ViewHolder(inflate) {
            };
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            viewHolder.setIsRecyclable(false);
            final CheckBox checkBox = viewHolder.itemView.findViewById(R.id.checkbox);
            TextView tvContent = viewHolder.itemView.findViewById(R.id.tvContent);
            final String str = mSelectItem.get(i);
            if (mEnable) {
                checkBox.setChecked(!TextUtils.isEmpty(mSelectValue.get(0)) && mSelectValue.get(0).equals(str));
                tvContent.setText(Tools.checkedString(str));
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        //单选
                        if (isChecked) {
                            mSelectValue.set(0, str);
                        } else {
                            mSelectValue.set(0, null);
                        }
                        new Handler().post(new Runnable() {
                            @Override
                            public void run() {
                                LogUtil.d(mSelectValue.toString());
                                notifyDataSetChanged();
                            }
                        });
                    }
                });
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (checkBox.isChecked()) {
                            mSelectValue.set(0, null);
                        } else {
                            mSelectValue.set(0, str);
                        }
                        notifyDataSetChanged();
                    }
                });
            } else {
                mSelectValue.set(0, null);
                tvContent.setText(Tools.checkedString(str));
                checkBox.setEnabled(false);
            }
        }

        @Override
        public int getItemCount() {
            return mSelectItem == null ? 0 : mSelectItem.size();
        }

        public void setEnable(boolean enable) {
            mEnable = enable;
            notifyDataSetChanged();
        }
    }
}
